package b.utils.entity

import akka.Done
import com.lightbend.lagom.scaladsl.api.transport.Forbidden
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity

/**
  * 贲国称 于 2017/1/13 创建。
  */
abstract class ACPersistentEntity extends PersistentEntity {
  def f命令处理[RT](ctx: CommandContext[RT], b: Boolean, msg: String, result: Persist): Persist = {
    if (b) {
      ctx.commandFailed(Forbidden(msg))
      ctx.done[Event]
    } else result
  }
  def fForbidden[RT](msg: String)(implicit ctx: CommandContext[RT]): Persist = {
    ctx.commandFailed(Forbidden(msg))
    ctx.done[Event]
  }
  def f命令Done: PartialFunction[(Command, CommandContext[Done], State), Persist] = {
    case (_, ctx, _) => ctx.done[Event]
  }
  def persistDone(event: Event)(implicit ctx: CommandContext[Done]): Persist = {
    ctx.thenPersist(event)(_ => ctx.reply(Done))
  }
  def tryPersistDone(event: Event)(implicit ctx: CommandContext[Done]): Persist = {
    try {
      ctx.thenPersist(event)(_ => ctx.reply(Done))
    } catch {
      case t: Throwable =>
        ctx.commandFailed(Forbidden(t.getMessage))
        ctx.done[Event]
    }
  }
  type TypePersistDone = PartialFunction[(Command, CommandContext[Done], State), Persist]
}

